//
//  LoginViewController.m
//  BigJoy
//
//  Created by Rowling on 2017/6/20.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "LoginViewController.h"
#import "XMGLoginField.h"
#import "VerifyPhoneVC.h"
#import "BindPhoneVC.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet XMGLoginField *phonNumTextField;
@property (weak, nonatomic) IBOutlet XMGLoginField *passwordTextField;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *loginToPasswordConstraint;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

-(void)setUI {
    [super viewWillLayoutSubviews];
    self.loginToPasswordConstraint.constant = XMW(61);
    UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"phone"]];
    UIView * view1 = [[UIView alloc]init];
    [view1 addSubview:imageView];
    view1.frame = CGRectMake(0, 0, imageView.width + 10, imageView.height+5);
    self.phonNumTextField.leftView = view1;
    self.phonNumTextField.leftViewMode = UITextFieldViewModeAlways;
    UIImageView * imageViewtwo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"suo"]];
    UIView * view2 = [[UIView alloc]init];
    view2.frame = CGRectMake(0, 0, imageView.width + 10, imageView.height+5);
    [view2 addSubview:imageViewtwo];
    self.passwordTextField.leftView = view2;
    self.passwordTextField.leftViewMode = UITextFieldViewModeAlways;
}

#pragma -mark:buttonClick
- (IBAction)backButtionClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)regiterButtonClick:(UIButton *)sender {
    VerifyPhoneVC * vc = [[VerifyPhoneVC alloc]init];
    vc.passType = normal;
    [self.navigationController pushViewController:vc animated:YES];
}
// 第三方登陆
- (IBAction)thirdLogButtonClick:(UIButton *)sender {
    LoginType type;
    NSInteger tag = sender.tag - 200;
    NSNumber * thirType;
    
    if (tag == 0) {
        type = WexinLogin;
        thirType = @3;
    }else if (tag == 1) {
        type = QQLogin;
        thirType = @2;
    }else {
        type = WeiBoLogin;
        thirType = @4;
    }
    XMweakSelf;
    [XMShareManager doLongin:type loginSuccessBlock:^(id result){
        NSLog(@"%@",result);
        [weakSelf thirdLoginOnOurSever:result type:thirType];
    }];
}
// 第三方登陆，去服务器上注册
-(void)thirdLoginOnOurSever:(NSDictionary *)data type:(NSNumber *)type {
    NSDictionary * dic = @{
                           @"states" : type,	//yes	第三方登录标识	post2=QQ 3=微信 4=微博
                           @"openID" :	data[@"open_id"],
                           @"registration_id" : @"UUID",
                           @"nickname" : data[@"name"],
                           @"photo" : data[@"head_photo"],
                           @"signature" : @"这家伙很懒,什么都没写。"
                           };
    [WYNetTool GET_Urlstring:ThirdLoginUrl parameters:dic success:^(id responseObject) {
        if (SuccessCode) {
            NSDictionary * data = responseObject[@"data"];
            [UserTool saveTheUserInfo:data];
            if ([UserTool getTheUserModel].phone.length < 4) {
                [self bindPhone];
            } else {
                [self.navigationController dismissViewControllerAnimated:YES completion:nil];
            }
        }
        [Apputil dismiss];
    } fail:^(id error) {
        
    }];
}

//绑定电话
-(void)bindPhone {
    VerifyPhoneVC * vc = [VerifyPhoneVC new];
    vc.passType = BindPhon;
    [self.navigationController pushViewController:vc animated:YES];
}
//登陆
- (IBAction)loginButtonClick:(id)sender {
    if (![XMSuperHelper isMobileNumber:self.phonNumTextField.text]) {
        [Apputil showError:@"请正确的输入您的手机号"];
        return;
    }
    if (self.passwordTextField.text.length == 0) {
        [Apputil showError:@"请输入您的密码"];
        return;
    }
    [SVProgressHUD showWithStatus:@"登陆中"];
    [UserTool LoginWithAC:self.phonNumTextField.text pwd:self.passwordTextField.text block:^(BOOL success){
        if (success) {
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
            [SVProgressHUD dismiss];
        }
    }];
}
// 忘记密码
- (IBAction)forgetPwd:(id)sender {
    VerifyPhoneVC * vc = [VerifyPhoneVC new];
    vc.passType = FindBack;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
